package org.mokusakura.domain;

import java.io.Serializable;
import java.math.BigDecimal;

public class CartItem implements Serializable {
    private static final long serialVersionUID = 6620528781626504362L;
    private Integer userId;
    private String itemId;
    private Item item;
    private Integer quantity;
    private Boolean inStock;
    private BigDecimal total;

    public Integer getUserId() {
        return userId;
    }

    public CartItem setUserId(Integer userId) {
        this.userId = userId;
        return this;
    }

    public String getItemId() {
        return itemId;
    }

    public CartItem setItemId(String itemId) {
        this.itemId = itemId;
        return this;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public CartItem setQuantity(Integer quantity) {
        this.quantity = quantity;
        return this;
    }

    public Boolean isInStock() {
        return inStock;
    }

    public CartItem setInStock(Boolean inStock) {
        this.inStock = inStock;
        return this;
    }


    public BigDecimal getTotal() {
        calculateTotal();
        return total;
    }

    public Item getItem() {
        return item;
    }

    public CartItem setItem(Item item) {
        this.item = item;
        return this;
    }


    private void calculateTotal() {
        if (item != null && item.getListPrice() != null) {
            total = item.getListPrice().multiply(new BigDecimal(quantity));
        } else {
            total = null;
        }
    }

    public void incrementQuantity() {
        quantity++;
    }

    @Override
    public String toString() {
        return "CartItem{" +
                "userId=" + userId +
                ", itemId='" + itemId + '\'' +
                ", item=" + item +
                ", quantity=" + quantity +
                ", inStock=" + inStock +
                ", total=" + total +
                '}';
    }
}
